/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306;

import com.ywh.train.Util;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.ImageIcon;
import my12306.bean.QueryResult;
import my12306.http.Consts;
import my12306.http.HttpClientUtils;
import my12306.ocr.OCR;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.jdesktop.application.Action;
import org.slf4j.LoggerFactory;

/**
 *
 * @author JiangQinhua
 */
public class Debug extends javax.swing.JFrame {

    private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
    TrainClient trainClient;
    HttpClient httpClient;
    final Object waitLock = new Object();
    boolean stop = false;

    /**
     * Creates new form Debug
     */
    public Debug() {
        initComponents();
        httpClient = HttpClientUtils.getHttpsClient();
        trainClient = new TrainClient(httpClient);
        init();

    }

    private void init() {
        Thread updateImage = new Thread(new Runnable() {
            public void run() {
                do {
                    if (stop) {
                        break;
                    }
                    updateImage();
                    log.info("waiting for signal...");
                    waitForSignal();
                    log.info("waiting for signal... signal received");
                } while (!stop);
            }
        }, "updateImage");

        updateImage.start();
        labRandcodeImg.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                continueRun();
            }
        });
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((size.width - getSize().width) / 2, (size.height - getSize().height) / 2);
    }

    private void updateImage() {
        try {
            byte[] image = null;// trainClient.getRandCodeBytes(Constants.LOGIN_CODE_URL);
            int count = 0;
            do {
                log.info("loading image {}", count++);
                image = trainClient.getRandCodeBytes(Consts.URL_passCodeAction_rand_login);
            } while (image == null || image.length == 0);
            labRandcodeImg.setIcon(new ImageIcon(image));
            labRandcodeImg.setText(OCR.doOCR(image));
        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage(), ex);
        } catch (Error ex) {
            log.error(ex.getLocalizedMessage(), ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtRandcode = new javax.swing.JTextField();
        labRandcodeImg = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtRandStr = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtSession1 = new javax.swing.JTextField();
        txtSession2 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtUrl = new javax.swing.JTextArea();
        jButton3 = new javax.swing.JButton();
        jRadioButtonPost = new javax.swing.JRadioButton();
        jRadioButtonGet = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getResourceMap(Debug.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtUsername.setText(resourceMap.getString("txtUsername.text")); // NOI18N
        txtUsername.setName("txtUsername"); // NOI18N

        txtRandcode.setText(resourceMap.getString("txtRandcode.text")); // NOI18N
        txtRandcode.setName("txtRandcode"); // NOI18N

        labRandcodeImg.setText(resourceMap.getString("labRandcodeImg.text")); // NOI18N
        labRandcodeImg.setName("labRandcodeImg"); // NOI18N
        labRandcodeImg.setPreferredSize(new java.awt.Dimension(20, 20));

        txtPassword.setText(resourceMap.getString("txtPassword.text")); // NOI18N
        txtPassword.setName("txtPassword"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getActionMap(Debug.class, this);
        jButton1.setAction(actionMap.get("getRandStrAction")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("loginAction")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtRandStr.setText(resourceMap.getString("txtRandStr.text")); // NOI18N
        txtRandStr.setName("txtRandStr"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        txtSession1.setText(resourceMap.getString("txtSession1.text")); // NOI18N
        txtSession1.setName("txtSession1"); // NOI18N

        txtSession2.setText(resourceMap.getString("txtSession2.text")); // NOI18N
        txtSession2.setName("txtSession2"); // NOI18N

        jButton4.setAction(actionMap.get("importAction")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        jButton5.setAction(actionMap.get("getSessionAction")); // NOI18N
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jButton1)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton2))
                                .addComponent(txtRandcode, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 35, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtRandStr, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(labRandcodeImg, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(231, 231, 231)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSession1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(txtSession2, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtSession1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtSession2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtRandcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labRandcodeImg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel5)
                        .addComponent(txtRandStr, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton4)
                        .addComponent(jButton5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(21, 21, 21))
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jEditorPane1.setContentType("text/html"); // NOI18N
        jEditorPane1.setName("jEditorPane1"); // NOI18N
        jScrollPane1.setViewportView(jEditorPane1);

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        txtUrl.setColumns(20);
        txtUrl.setRows(5);
        txtUrl.setName("txtUrl"); // NOI18N
        jScrollPane2.setViewportView(txtUrl);

        jButton3.setAction(actionMap.get("testAction")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        buttonGroup1.add(jRadioButtonPost);
        jRadioButtonPost.setText(resourceMap.getString("jRadioButtonPost.text")); // NOI18N
        jRadioButtonPost.setName("jRadioButtonPost"); // NOI18N

        buttonGroup1.add(jRadioButtonGet);
        jRadioButtonGet.setSelected(true);
        jRadioButtonGet.setText(resourceMap.getString("jRadioButtonGet.text")); // NOI18N
        jRadioButtonGet.setName("jRadioButtonGet"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jRadioButtonGet)
                .addGap(18, 18, 18)
                .addComponent(jRadioButtonPost)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 865, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonPost)
                    .addComponent(jRadioButtonGet)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 3, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Debug.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Debug.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Debug.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Debug.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Debug().setVisible(true);
            }
        });
    }

    @Action
    public void getRandStrAction() {
        txtRandStr.setText(trainClient.loginAction_loginAysnSuggest());

    }

    @Action
    public void loginAction() {
        QueryResult qr = trainClient.login(txtUsername.getText().trim(),
                txtPassword.getText().trim(),
                txtRandcode.getText().trim(),
                txtRandStr.getText().trim());
        getcookies();
        log(qr);
    }

    private void getcookies() {
        List<Cookie> cookies = ((DefaultHttpClient) httpClient).getCookieStore().getCookies();
        StringBuilder sb = new StringBuilder();
        if (cookies.size() == 2) {
            jLabel6.setText(cookies.get(0).getName());
            txtSession1.setText(cookies.get(0).getValue());
            jLabel7.setText(cookies.get(1).getName());
            txtSession2.setText(cookies.get(1).getValue());
        }
        for (Cookie cookie : cookies) {
            log.info("Cookie: {} : {}", cookie.getName(), cookie.getValue());
            sb.append(String.format(";%s=%s", cookie.getName(), cookie.getValue()));
        }
        log(sb.toString());
    }

    public HttpRequestBase getHttpMethod(String url) {
        HttpRequestBase result = null;
        if (jRadioButtonGet.isSelected()) {
            result = trainClient.getGet(url);
        } else {
            result = trainClient.getPost(url);
        }
        return result;
    }

    public QueryResult execute(HttpRequestBase request) {
        QueryResult qr = new QueryResult();
        try {
            String result = httpClient.execute(request, new BasicResponseHandler());
            String info = Util.removeTagFromHtml(result);
            qr.setRawData(result);
            qr.setMsg(info);
            qr.setState(QueryResult.SUCC);
        } catch (Exception ex) {
            qr.setState(QueryResult.FAIL);
            qr.setMsg(ex.getMessage());
            log.error(ex.getLocalizedMessage(), ex);
        } finally {
        }
        return qr;
    }

    @Action
    public void testAction() {
        String url = txtUrl.getText().trim();
        if (url.isEmpty()) {
            return;
        }
        HttpRequestBase request = getHttpMethod(url);
        QueryResult result = execute(request);
        log(result);
    }

    public synchronized void log(QueryResult result) {
        if (result.getState() == QueryResult.SUCC) {
            int index = result.getRawData().indexOf("<body");
            if (index > 0) {
                log(result.getRawData().substring(index));
            } else {
                log(result.getRawData());
            }
        } else {
            log(result.getMsg());
        }
    }

    public synchronized void log(String msg) {
        try {
            jEditorPane1.setText(msg);
            log.info(msg);
        } catch (Exception ex) {
        }
    }

    void continueRun() {
        synchronized (waitLock) {
            log.info("sending signal...");
            waitLock.notifyAll();
            log.info("sending signal... done");

        }
    }

    private void waitForSignal() {
        synchronized (waitLock) {
            try {
                waitLock.wait();
            } catch (InterruptedException ie) {
                // continue
            }
        }
    }

    public void setStop() {
        stop = true;
        continueRun();
    }

    @Action
    public void importAction() {
        List<Cookie> cookies = ((DefaultHttpClient) httpClient).getCookieStore().getCookies();

        ((DefaultHttpClient) httpClient).getCookieStore().clear();
        for (Cookie cookie1 : cookies) {
            BasicClientCookie cookie = (BasicClientCookie) cookie1;
            if (cookie.getName().equals("BIGipServerotsweb")) {
                cookie.setValue(txtSession1.getText().trim());
            } else if (cookie.getName().equals("JSESSIONID")) {
                cookie.setValue(txtSession2.getText().trim());
            }

            ((DefaultHttpClient) httpClient).getCookieStore().addCookie(cookie);
        }
    }

    @Action
    public void getSessionAction() {
//        getcookies();
        List<Cookie> cookies = ((DefaultHttpClient) httpClient).getCookieStore().getCookies();

        for (Cookie cookie1 : cookies) {
            BasicClientCookie cookie = (BasicClientCookie) cookie1;
            if (cookie.getName().equals("BIGipServerotsweb")) {
                txtSession1.setText(cookie.getValue());
            } else if (cookie.getName().equals("JSESSIONID")) {
                txtSession2.setText(cookie.getValue());
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButtonGet;
    private javax.swing.JRadioButton jRadioButtonPost;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labRandcodeImg;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JLabel txtRandStr;
    private javax.swing.JTextField txtRandcode;
    private javax.swing.JTextField txtSession1;
    private javax.swing.JTextField txtSession2;
    private javax.swing.JTextArea txtUrl;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
